package com.csu.jpetstore_reconfiguration_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csu.jpetstore_reconfiguration_back.common.CommonResponse;
import com.csu.jpetstore_reconfiguration_back.domain.Category;
import com.csu.jpetstore_reconfiguration_back.service.CategoryService;
import com.csu.jpetstore_reconfiguration_back.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 朱嘉辉
* @description 针对表【t_category】的数据库操作Service实现
* @createDate 2024-04-13 20:26:20
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Autowired
    CategoryMapper categoryMapper;

    //获取所有分类
    @Override
    public CommonResponse<List<Category>> getAllCategory() {
        List<Category> categoryList=categoryMapper.selectList(null);
        if(categoryList.isEmpty())
        {
            return CommonResponse.createForSuccessMessage("没有分类信息");
        }
        return CommonResponse.createForSuccess(categoryList);
    }

    //获取单个分类

    @Override
    public CommonResponse<Category> getCategory(String categoryId) {
        Category category=categoryMapper.selectById(categoryId);
        if(category==null)
        {
            return CommonResponse.createForSuccessMessage("没有该ID的Category");
        }
        return CommonResponse.createForSuccess(category);
    }

    @Override
    public CommonResponse<Category> addCategory(Category category) {

        QueryWrapper<Category>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("category_id",category.getCategoryId());
        if(categoryMapper.selectList(queryWrapper).isEmpty())
        {
            categoryMapper.insert(category);
            return CommonResponse.createForSuccess("添加成功",category);
        }
        return CommonResponse.createForError("该CategoryID已存在");

    }

    @Override
    public CommonResponse<Category> updateCategory(String categoryId,Category category) {
        UpdateWrapper<Category>updateWrapper=new UpdateWrapper<>();
        updateWrapper.like("category_id",categoryId);
        updateWrapper.set("category_id",category.getCategoryId()).set("name",category.getName()).set("descn",category.getDescn()).set("img_url",category.getImgUrl());
        categoryMapper.update(category,updateWrapper);

        return CommonResponse.createForSuccess("对"+categoryId+"修改成功",category);
    }

    @Override
    public CommonResponse<Category> deleteCategory(String categoryId) {
        QueryWrapper<Category>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryId);
        Category category=categoryMapper.selectById(categoryId);
        categoryMapper.delete(queryWrapper);
        return CommonResponse.createForSuccess("删除成功",category);
    }
}




